package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.AdSensitiveMapper;
import com.heima.admin.service.AdSensitiveService;
import com.heima.common.exception.CustException;
import com.heima.model.admin.dtos.SensitiveDto;
import com.heima.model.admin.pojos.AdSensitive;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * @author Jt
 * @date 2021/4/10 22:30
 */
@Service
@Transactional
public class AdSensitiveServiceImpl extends ServiceImpl<AdSensitiveMapper, AdSensitive> implements AdSensitiveService {
    /**
     * 查询敏感词
     * @param sensitiveDto
     * @return
     */
    @Override
    public ResponseResult list(SensitiveDto sensitiveDto) {
//        1.检查参数
        if (sensitiveDto == null) {
            CustException.cust(AppHttpCodeEnum.PARAM_INVALID);
        }
//        2.检查分页
        sensitiveDto.checkParam();
//        3.查询条件的封装 分页信息
        Page<AdSensitive> page = new Page<>(sensitiveDto.getPage(), sensitiveDto.getSize());
//        4.模糊查询
        LambdaQueryWrapper<AdSensitive> lambdaQueryWrapper = Wrappers.lambdaQuery();
//        进行名称的判断
        if (StringUtils.isNotBlank(sensitiveDto.getName())) {
            lambdaQueryWrapper.like(AdSensitive::getSensitives,sensitiveDto.getName());
        }
        IPage<AdSensitive> pageResult = page(page, lambdaQueryWrapper);
//        分页结果封装
        PageResponseResult result = new PageResponseResult(sensitiveDto.getPage(), sensitiveDto.getSize(), (int) pageResult.getTotal());
        result.setData(pageResult.getRecords());
        return result;
    }

    /**
     * 新增敏感词
     * @param adSensitive
     * @return
     */
    @Override
    public ResponseResult insert(AdSensitive adSensitive) {
//        1.检查参数
        if (adSensitive == null || adSensitive.getSensitives() == null) {
            CustException.cust(AppHttpCodeEnum.PARAM_INVALID);
        }
//        2.新增
        adSensitive.setCreatedTime(new Date());
        save(adSensitive);
//        3.返回结果
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 修改敏感词
     * @param adSensitive
     * @return
     */
    @Override
    public ResponseResult update(AdSensitive adSensitive) {
//        1.检查参数
        if (adSensitive == null || adSensitive.getId() == null) {
            CustException.cust(AppHttpCodeEnum.PARAM_INVALID);
        }
        AdSensitive sensitive = getById(adSensitive.getId());
        if (sensitive == null) {
            CustException.cust(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
//        修改
        updateById(adSensitive);
//        返回结果
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 根据ID删除敏感词
     * @param id
     * @return
     */
    @Override
    public ResponseResult delete(Integer id) {
//        1.检查参数
        if (id == null) {
            CustException.cust(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
//        2.检查敏感词是否存在
        AdSensitive sensitive = getById(id);
        if (sensitive == null) {
            CustException.cust(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
//        3.删除
        removeById(id);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
